//=============================================================================
// Grenade.
//=============================================================================
class TFMMProjectile extends TFProjectile;

var xEmitter Trail, beam;
var() float DampenFactor, DampenFactorParallel;
var class<xEmitter> HitEffectClass;
var float LastSparkTime;
var sound ImpactSounds[6];

replication
{
    reliable if( Role==ROLE_Authority )
	trail, beam;
}

simulated function Destroyed()
{
    if (Trail!=None) Trail.mRegen=False;
    if (beam!=none) Beam.setbase(none);
    Super.Destroyed();
}

simulated function PostBeginPlay()
{
    if ( EffectIsRelevant(Location,false) )
    {
        Trail=Spawn(class'TaskForces.TFMMTrail',self);

        Beam=Spawn(class'TaskForces.TFMMBeamFX',self);
        Beam.SetBase(self);
        Beam.mSpawnVecA=Location;

        spawn(class'XEffects.ShockMuzFlash3rd');
    }

    Super.PostBeginPlay();
}

simulated function ProcessTouch (Actor Other, vector HitLocation)
{
    local float spd, dmg;
    local xemitter fx;

    spd=vsize(Velocity);
    dmg=fmax(0,default.damage*(spd/default.speed));
    MomentumTransfer=default.MomentumTransfer*(spd/default.speed);
    if(pawn(other)==none) dmg*=1.3;

    if((dmg/default.damage)>=2.9) mydamagetype=class'DamTypeMMsplash';

    if ( TFBlockshield(Other)==None && TFMMProjectile(Other)==None && Other!=Instigator )
    {
        if ( Role == ROLE_Authority )
            Other.TakeDamage( dmg, Instigator, HitLocation,
            (MomentumTransfer * Velocity/speed), MyDamageType );

        if(pawn(other)!=none)
        {
            if ( spd > 20000)
                PlaySound(ImpactSounds[3], SLOT_Misc ,6);
	        else if (spd > 7000)
	            PlaySound(ImpactSounds[4], SLOT_Misc ,1.5);
            else if (spd > 100)
		        PlaySound(ImpactSounds[5], SLOT_Misc );

		    if(pawn(other).health>0 && spd<20000) Destroy();
            else velocity*=0.6;
		}
        else
        {
            if ( Speed > 20000 )
	            PlaySound(ImpactSounds[0], SLOT_Misc ,4);

	        if(EffectIsRelevant(HitLocation,false))
            {
                fx=Spawn(class'taskforces.tfmmfragflash',,, HitLocation);
                fx.mStartParticles=3+(spd/default.speed);
                fx.mGrowthrate*=spd/default.speed;
                fx.Trigger(self, instigator);
            }

	        Destroy();
	    }
    }
}

simulated function PhysicsVolumeChange( PhysicsVolume Volume )
{
    if (Volume.bWaterVolume)
    {
        Velocity *= 0.75;
    }
}

simulated function Tick(float dt)
{
    if(trail!=none)
    {
        Trail.mSpeedRange[0]=-300*(vsize(Velocity)/default.speed);
        Trail.mSpeedRange[1]=-300*(vsize(Velocity)/default.speed);
        Trail.mRegenRange[0]=25+75*(vsize(Velocity)/default.speed);
        Trail.mRegenRange[1]=25+75*(vsize(Velocity)/default.speed);

        Trail.mDirDev=vect(0.03,0.03,0.03)*(3.6-vsize(Velocity)/default.speed);
    }

    super.Tick(dt);
}

simulated function HitWall( vector HitNormal, actor Wall )
{
    local Vector VNorm;
    local float power;
    local xEmitter fx;

    if (beam!=none) Beam.setbase(none);

    if ( (Mover(Wall) != None) && Mover(Wall).bDamageTriggered )
    {
        if ( Level.NetMode != NM_Client )
            Wall.TakeDamage( Damage, instigator, Location, MomentumTransfer * Normal(Velocity), MyDamageType);
    }

    power=vsize(velocity)/default.speed;

    PlaySound(ImpactSounds[0], SLOT_Misc ,2*power);

    if ( EffectIsRelevant(Location,false) )
    {
        Spawn(HitEffectClass,,, Location, Rotator(HitNormal));

        Spawn(class'taskforces.tfwbdecal',,,,rotator(-HitNormal));
		Spawn(ExplosionDecal,self,,, rotator(-HitNormal));

        fx=Spawn(class'taskforces.tfmmfragflash',,, Location, Rotator(HitNormal));
        fx.mStartParticles=3+power;
        fx.mGrowthrate*=power;
        fx.Trigger(self, instigator);

        fx=Spawn(class'TaskForces.TFMMSparks',,,, Rotator(HitNormal));
        fx.mStartParticles=20+30*power;
        fx.mSpeedRange[0]*=power;
        fx.mSpeedRange[1]*=power;
        fx.Trigger(self, instigator);

        VNorm=(Velocity dot HitNormal)*HitNormal;
        SetRotation(rotator(normal(-VNorm*DampenFactor+(Velocity-VNorm)*DampenFactorParallel)));

        fx=Spawn(class'TaskForces.TFMMDirSparks');
        fx.mStartParticles=10;
        fx.mSpeedRange[0]*=power;
        fx.mSpeedRange[1]*=power;
        fx.Trigger(self, instigator);
    }

    HurtRadius(power*30, power*200, class'DamTypeMMHit', 2000, Location);

    destroy();
}

defaultproperties
{
     DampenFactor=0.600000
     DampenFactorParallel=0.900000
     HitEffectClass=Class'taskforces.TFMMImpRing'
     ImpactSounds(0)=Sound'TFMedia.Marksman.MMHitWall1'
     ImpactSounds(1)=Sound'TFMedia.Marksman.MMHitWall2'
     ImpactSounds(2)=Sound'TFMedia.Marksman.MMHitWall3'
     ImpactSounds(3)=Sound'TFMedia.Marksman.MMHitFlesh1'
     ImpactSounds(4)=Sound'TFMedia.Marksman.MMHitFlesh2'
     ImpactSounds(5)=Sound'TFMedia.Marksman.MMHitFlesh3'
     Speed=7000.000000
     MaxSpeed=999999.000000
     Damage=90.000000
     MomentumTransfer=80000.000000
     MyDamageType=Class'taskforces.DamTypeMMHit'
     ImpactSound=ProceduralSound'WeaponSounds.PGrenFloor1.P1GrenFloor1'
     ExplosionDecal=Class'XEffects.LinkBoltScorch'
     DrawType=DT_StaticMesh
     StaticMesh=StaticMesh'XEffects.EffectsSphere144'
     LifeSpan=10.000000
     DrawScale=0.100000
     Skins(0)=TexEnvMap'TFMedia.Buildings.TFBshine'
     AmbientGlow=64
     CollisionRadius=4.000000
     CollisionHeight=4.000000
     bBounce=True
}
